package apptoko.com;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import apptoko.com.fragment.CategoryListFragment;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import apptoko.com.R;
import com.astuetz.viewpager.extensions.PagerSlidingTabStrip;
import com.google.analytics.tracking.android.EasyTracker;

public class ListingActivity extends SherlockFragmentActivity {
	
	public static final String TOP = "cat_top";
	public static final String KEY_CAT_ID = "_cat_id";
	public static final String KEY_CAT_TITLE = "_cat_title";
	private ViewPager viewPager;
	private String currentStore;
	private Resources res;
	private CategoryFragmentAdapter adapter;
	private int catId;
	private String catTitle;
	private String store;
	private int indicatorColor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		store = getIntent().getStringExtra(Constants.STORE_KEY);
		if(store == null){
			store = "apps";
		}
//		if(store.equalsIgnoreCase("apps")){
			setTheme(R.style.Theme_Styled_Home);
			setThemeLowEndDevice(R.drawable.action_bar_home);
			indicatorColor = R.color.app_indicator;
//		} else if(store.equalsIgnoreCase("books")){
//			setTheme(R.style.Theme_Styled_Book);
//			setThemeLowEndDevice(R.drawable.action_bar_book);
//			indicatorColor = R.color.ebook_indicator;
//		} else if(store.equalsIgnoreCase("comics")){
//			setTheme(R.style.Theme_Styled_Comic);
//			setThemeLowEndDevice(R.drawable.action_bar_comic);
//			indicatorColor = R.color.comic_indicator;
//		} else if(store.equalsIgnoreCase("movies")){
//			setTheme(R.style.Theme_Styled_Movie);
//			setThemeLowEndDevice(R.drawable.action_bar_movie);
//			indicatorColor = R.color.film_indicator;
//		} else if(store.equalsIgnoreCase("games")){
//			setTheme(R.style.Theme_Styled_Apps);
//			setThemeLowEndDevice(R.drawable.action_bar_apps);
//			indicatorColor = R.color.app_indicator;
//		} else if(store.equalsIgnoreCase("gift")){
//			setTheme(R.style.Theme_Styled_Gift);
//			setThemeLowEndDevice(R.drawable.action_bar_gift);
//			indicatorColor = R.color.app_indicator;
//		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_pager);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		initVariables();
		initViews();
		EasyTracker.getInstance().setContext(this);
	}
	
	private void setThemeLowEndDevice(int drawableId){
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(drawableId);
            bg.setTileModeX(TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);
        }
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		com.facebook.AppEventsLogger.activateApp(this, getResources().getString(R.string.fb_app_id));
	}
	
	private void initVariables(){
		 currentStore = getIntent().getStringExtra(Constants.STORE_KEY);
		 res = getResources();
		 catId = getIntent().getIntExtra(Constants.CATEGORY_ID, 0);
		 catTitle = getIntent().getStringExtra(Constants.CATEGORY_NAME);
		 getSupportActionBar().setTitle(catTitle);
	}
	
	private void initViews(){
		viewPager = (ViewPager) findViewById(R.id.pager);
		DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int pageMargin = metrics.widthPixels / 48;
		viewPager.setOffscreenPageLimit(4);
		viewPager.setPageMargin(pageMargin);
		viewPager.setPageMarginDrawable(R.drawable.bg_pagemargin_xml);
		adapter = new CategoryFragmentAdapter(getSupportFragmentManager());
		viewPager.setAdapter(adapter);
		PagerSlidingTabStrip indicator = (PagerSlidingTabStrip) findViewById(R.id.indicator);
        indicator.setViewPager(viewPager);
        indicator.setIndicatorColorResource(R.color.indicator_color);
        int pixelSize = (int) (14 * metrics.scaledDensity); 
        indicator.setTextSize(pixelSize);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getSupportMenuInflater().inflate(R.menu.activity_home, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		case R.id.menu_search:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "search-button", Long.valueOf(10));
			Intent search = new Intent(this, SearchActivity.class);
			search.putExtra(Constants.SEARCH_TYPE, store);
			startActivity(search);
			return true;
		case R.id.menu_account:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "menu-account", Long.valueOf(1));
			boolean isLogin = Utils.getStaticIsLogin(this);
			if (isLogin) {
				Intent account = new Intent(ListingActivity.this, AccountActivity.class);
				startActivity(account);
			} else {
				Intent login = new Intent(ListingActivity.this, LoginActivity.class);
				startActivity(login);
			}
			return true;
		case R.id.menu_manage:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "menu-manage", Long.valueOf(1));
			Intent i = new Intent(ListingActivity.this, AppManageActivity.class);
			i.putExtra(Constants.STORE_KEY, store);
			startActivity(i);
			return true;
		case R.id.menu_setting:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "menu-setting", Long.valueOf(1));
			Intent setting = new Intent(ListingActivity.this, SettingsActivity.class);
			startActivity(setting);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//EasyTracker.getInstance().activityStart(this);
	}
	
	private class CategoryFragmentAdapter extends FragmentStatePagerAdapter {
		
		protected String[] TITLE = new String[] { res.getString(R.string.top_new), res.getString(R.string.top_download)};

		public CategoryFragmentAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int pos) {
			CategoryListFragment frag = new CategoryListFragment();
			Bundle bundle = new Bundle();
			bundle.putString(Constants.TOP, TITLE[pos]);
			bundle.putInt(Constants.CATEGORY_ID, catId);
			bundle.putString(Constants.CATEGORY_NAME, catTitle);
			bundle.putString(Constants.STORE_KEY, currentStore);
			frag.setArguments(bundle);
			return frag;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return TITLE.length;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return TITLE[position];
		}
		
		@Override
		public void setPrimaryItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			if(position > 0){
				CategoryListFragment frag = (CategoryListFragment) object;
				frag.init();
			}
		}
	}
}
